<template>
	<view class="login-wrap">
		<!-- <image class="login-img" src="https://ya30images.hbwlife.com/wxmp/logo-yongan30.png" />
		<text class="login-txt">登录查看更多信息</text> -->
		<!-- <button class="add-btn" @tap="handleLogin">登录</button> -->
	</view>
</template>

<script>
	import {
		requestData
	} from '../../utils/http.js'
	export default {
		data() {
			return {
				currentPath: '',
			}
		},
		onLoad(options) {
			if (options.currentPath) {
				this.currentPath = decodeURIComponent(options.currentPath)
			}
			this.handleLogin()
		},
		methods: {
			handleLogin() {
				let that = this
				uni.login({
					provider: 'weixin', //微信登录
					success: function(loginRes) {
						let code = loginRes?.code
						if (code) {
							requestData('/wxaccess/mp/user/login', {
									wxlogin_code: code,
								})
								.then((res) => {
									let user_id = res?.data?.datas?.user_id || '';
									let token = res?.data?.datas?.access_token || '';
									if (user_id && token) {
										uni.setStorageSync('access_token', token)
										uni.setStorageSync('user_id', user_id)
										if (
											that.currentPath == 'pages/home/index' ||
											that.currentPath == 'pages/site/index' ||
											that.currentPath == 'pages/message/index' ||
											that.currentPath == 'pages/my/index'
										) {
											//在tab页token失效,刷新后，还跳转到本页
											uni.switchTab({
												url: '/' + that.currentPath,
											})
										} else {
											//在非tab页token失效,刷新后，由于currentPath是pagesB/titckDetail/index，没有携带参数的,，所以先统一跳转到home页

											uni.switchTab({
												url: '/pages/home/index'
											})
											// uni.redirectTo({
											// 	url: '/' + that.currentPath,
											// })
										}
									} else {
										// uni.showToast({
										// 	title: '请先注册',
										// })
										//目前应该走不到这了，因为在h5中tab页面onMounted中做判断了，直接跳转到登录手机号页面
										setTimeout(() => {
											uni.redirectTo({
												url: '/pages/loginPhone/loginPhone?currentPath=' +
													encodeURIComponent(this.currentPath),
											})
										}, 1000)
									}
								})
								.catch((err) => {
									console.log(err)
								})
						}
					},
					fail: function(err) {
						console.log(err)
					},
				})
			},
		},
	}
</script>

<style>
	.login-wrap {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
	}

	.login-img {
		width: 320rpx;
		height: 320rpx;
		object-fit: contain;
	}

	.login-txt {
		margin-bottom: 20rpx;
	}

	.add-btn {
		outline: none;
		border: none;
		width: 90%;
		height: 80rpx;
		line-height: 80rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 30rpx;
		color: #533504;
		background: linear-gradient(to right, #c8a357, #fbde94, #c8a357);
		font-size: 32rpx;
		font-weight: 400;
	}

	button::after {
		border: none;
	}
</style>